<?php
namespace App\Http\Controllers\ShopWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\ShopWeb\Auth\ShopWebAuth;
use App\Http\Controllers\Business\ShopWeb\PublicBusiness;
use App\Http\Controllers\Business\ShopWeb\OrderManagement\OrderManagement;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;
use Maatwebsite\Excel\Facades\Excel;

class OrderController extends WebController
{
	//订单管理
    public function orderIndex() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        $result = ShopWebAuth::getShopUsersByShopIds($shopIds);
        $shopUserLists = [
            [
                'shopuserid' => -1,
                'shopusername' => '全部',
                'shopusermobile' => '',
                'shopname' => '',
                'shopid' => '',
                'status' => '',
            ],
            [
                'shopuserid' => 0,
                'shopusername' => '默认',
                'shopusermobile' => '',
                'shopname' => '',
                'shopid' => '',
                'status' => '',
            ]
        ];
        if($result['status'] == 'success') {
            $lists = $result['data'];
            foreach($lists as $list) {
                array_push($shopUserLists, $list);
            }
        }
        if($this->request->has('shopaccountid')) {
            $shopAccountId = $this->request->get('shopaccountid');
        } else {
            $shopAccountId = 0;
        }
        $data = [
            'shopuserlists' => $shopUserLists,
            'shopaccountid' => $shopAccountId
        ];
        return view('shop.order.index', $data);
    }
    //订单列表
    public function orderList() {
        $loginUser = Auth::guard('shopweb')->user();
        $loginUserId = $loginUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $loginUserId);
        if(!$shopIdString) {
            $shopIdString = $loginUser->shopid;
        }
        $shopIdArray = explode(',', $shopIdString);
        if($this->request->has('ordernumber') && ($this->request->get('ordernumber') !='')) {
            $orderNumber = $this->request->get('ordernumber');
        } else {
            $orderNumber = '%';
        }
        if($this->request->has('minamount') && ($this->request->get('minamount') !='')) {
            $minAmount = $this->request->get('minamount');
        } else {
            $minAmount = 0.00;
        }
        if($this->request->has('maxamount') && ($this->request->get('maxamount') !='')) {
            $maxAmount = $this->request->get('maxamount');
        } else {
            $maxAmount = 9999;
        }
        if($this->request->has('shopuserid') && ($this->request->get('shopuserid') !='')) {
            $shopUserId = $this->request->get('shopuserid');
        } else {
            $shopUserId = 0;
        }
        if($this->request->has('starttime') && ($this->request->get('starttime') !='')) {
            $startTime = $this->request->get('starttime');
            $startTime = $startTime . " 00:00:00";
        } else {
            $startTime = '';
        }
        if($this->request->has('lasttime') && ($this->request->get('lasttime') !='')) {
            $lastTime = $this->request->get('lasttime');
            $lastTime = $lastTime . " 23:59:59";
        } else {
            $lastTime = '';
        }
        if($this->request->has('status') && ($this->request->get('status') !='')) {
            $status = $this->request->get('status');
        } else {
            $status = '%';
        }
        if($this->request->has('paginate') && ($this->request->get('paginate') !='')) {
            $paginate = $this->request->get('paginate');
        } else {
            $paginate = 50;
        }
        if($this->request->has('shopaccountid') && ($this->request->get('shopaccountid') != 0)) {
            $shopAccountId = $this->request->get('shopaccountid');
        } else {
            $shopAccountId = 0;
        }
        $result = OrderManagement::orderList($shopIdArray,$orderNumber,$minAmount,$maxAmount,$shopUserId,$startTime,$lastTime,$status,$paginate, $shopAccountId);
        if($result['status'] == 'failed') {
            return $this->responseError($result['message']);
        }
        return $this->responseSuccessData($result['data']);
    }
    //订单详情
    public function orderDetails() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        $orderId = $this->request->get('orderid');
        $result = OrderManagement::getOrderDetail($shopIds, $orderId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        return view('shop.order.order_details', $data);
    }
    //导出订单
    public function exportOrderList() {
        $loginUser = Auth::guard('shopweb')->user();
        $loginUserId = $loginUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $loginUserId);
        if(!$shopIdString) {
            $shopIdString = $loginUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        if($this->request->has('ordernumber') && ($this->request->get('ordernumber') !='')) {
            $orderNumber = $this->request->get('ordernumber');
        } else {
            $orderNumber = '%';
        }
        if($this->request->has('minamount') && ($this->request->get('minamount') !='')) {
            $minAmount = $this->request->get('minamount');
        } else {
            $minAmount = 0.00;
        }
        if($this->request->has('maxamount') && ($this->request->get('maxamount') !='')) {
            $maxAmount = $this->request->get('maxamount');
        } else {
            $maxAmount = 9999;
        }
        if($this->request->has('shopuserid') && ($this->request->get('shopuserid') !='')) {
            $shopUserId = $this->request->get('shopuserid');
        } else {
            $shopUserId = 0;
        }
        if($this->request->has('starttime') && ($this->request->get('starttime') !='')) {
            $startTime = $this->request->get('starttime');
            $startTime = $startTime . " 00:00:00";
        } else {
            $startTime = '';
        }
        if($this->request->has('lasttime') && ($this->request->get('lasttime') !='')) {
            $lastTime = $this->request->get('lasttime');
            $lastTime = $lastTime . " 23:59:59";
        } else {
            $lastTime = '';
        }
        if($this->request->has('status') && ($this->request->get('status') !='')) {
            $status = $this->request->get('status');
        } else {
            $status = '%';
        }
        if($this->request->has('shopaccountid') && ($this->request->get('shopaccountid') != 0)) {
            $shopAccountId = $this->request->get('shopaccountid');
        } else {
            $shopAccountId = 0;
        }
        $result = OrderManagement::exportOrderList($shopIds,$orderNumber,$minAmount,$maxAmount,$shopUserId,$startTime,$lastTime,$status, $shopAccountId);
        if($result['status'] == 'failed') {
            return $this->responseError('导出错误');
        }
        $data = $result['data'];
        $cellData = [
            ['订单时间','订单号','用户手机号','消费金额','结款状态','消费门店', '收银员']
        ];
        foreach($data as $list) {
            $showAccountStatus = ($list['accountstatus'] == 1) ? "已提现":"未提现";
            $newList = [
                'created_at' => $list['created_at'],
                'ordernumber' => $list['ordernumber'],
                'mobile' => $list['mobile'],
                'orderamount' => $list['orderamount'],
                'accountstatus' => $showAccountStatus,
                'shopname' => $list['shopname'],
                'shopusername' => $list['shopusername']
            ];
            array_push($cellData, $newList);
        }
        $title = '订单列表';
        Excel::create($title,function($excel) use ($cellData){
            $excel->sheet('订单列表', function($sheet) use ($cellData){
                $sheet->rows($cellData);
            });
        })->export('xls');
        return $this->responseSuccessMessage('导出成功');
    }
    
}